<?php
/**
 * Token激励模型基础
 * @App	
 * @Group 
 * @Action TokenExcitationBaseService
 * @author lilei
 * @date 2018年10月24日
 *
 */
namespace app\common\service;
class TokenExcitationBaseService {
	
	/**
	 * 注册用户数
	 * 
	 * @var int|number
	 */
	var $total_user;
	
	/**
	 * 总作者数
	 * 
	 * @var string
	 */
	var $total_author;
	
	/**
	 * 单日注册用户数
	 * 
	 * @var string
	 */
	var $daily_register_user;
	
	/**
	 * 每日合格文章数
	 */
	var $daily_article;
	
	/**
	 * 用户注册默认给与H矿
	 * 
	 * @var string
	 */
	var $user_register_token = 1;
	
	/**
	 * 一级被邀请用户数
	 * 
	 * @var string
	 */
	var $total_user_invite_1;
	
	/**
	 * 二级被邀请用户数
	 * 
	 * @var string
	 */
	var $total_user_invite_2;
	
	/**
	 * 三级被邀请用户数
	 * 
	 * @var string
	 */
	var $total_user_invite_3;
	
	/**
	 * 合格文章总数
	 */
	var $total_article;
	
	/**
	 * 使用时长
	 * 
	 * @var string
	 */
	var $Time;
	
	/**
	 * 浏览数
	 * 
	 * @var string
	 */
	var $visit;
	
	/**
	 * 后续浏览
	 * 
	 * @var string
	 */
	var $follow_visit;
	
	/**
	 * 被打赏文章后续浏览数
	 * 
	 * @var string
	 */
	var $tip_visit = 0;
	
	/**
	 * 未被打赏文章后续浏览数
	 * 
	 * @var string
	 */
	var $no_tip_visit = 0;
	
	/**
	 * 打赏H币数
	 * 
	 * @var string
	 */
	var $tip = 0;
	
	/**
	 * 推送服务消费的H币
	 * 
	 * @var string
	 */
	var $alert = 0;
	
	/**
	 * 其他消费的H币
	 * 
	 * @var string
	 */
	var $other = 0;
	
	/**
	 * H矿价格
	 * 
	 * @var string
	 */
	var $token_price;
	
	/**
	 * H矿价格系数(H矿价格调节参数)
	 * 
	 * @var string
	 */
	var $token_price_param;
	
	/**
	 * 单日获取H币上限(应该就是单日产生H币上限制)
	 * 0 标识不限制
	 * @var string
	 */
	var $daily_coin_limit = 0;
	
	/**
	 * 用户单次激励行为奖励额度(用户单次奖励额度)
	 * 
	 * @var string
	 */
	var $bonus;
	
	/**
	 * 作者单次激励行为奖励额度----被打赏文章单次浏览收益
	 * 
	 * @var string
	 */
	var $bonus1 = 0;
	
	/**
	 * 作者单次激励行为奖励额度----未被打赏文章单次浏览收益
	 * 
	 * @var string
	 */
	var $bonus2 = 0;
	
	/**
	 * 后续收益额度
	 * 
	 * @var string
	 */
	var $follow_bonus = 0;
	
	/**
	 * 成本总额(分红池)
	 * 
	 * @var string
	 */
	var $fund;
	
	/**
	 * 每个新的注册用户所需成本(默认5元)
	 * 
	 * @var string
	 */
	var $fund_user_price = 5;
	
	/**
	 * 每篇文章所需成本(默认5元)
	 * 
	 * @var string
	 */
	var $fund_article_price = 5;
	
	/**
	 * 总H币产生数
	 * 
	 * @var string
	 */
	var $total_produce_coin;
	
	/**
	 * 用户和作者H币总持有数
	 * 
	 * @var string
	 */
	var $total_coin;
	
	/**
	 * H币兑换总数量
	 * 
	 * @var string
	 */
	var $total_exchange_coin;
	
	/**
	 * H币消费总数量
	 * 
	 * @var string
	 */
	var $total_consume_coin;
	
	/**
	 * H币主激励产生数
	 * 
	 * @var string
	 */
	var $total_main_coin;
	
	/**
	 * H币总投资收益数
	 * 
	 * @var string
	 */
	var $total_follow_coin;
	
	/**
	 * H矿总释放数
	 * 
	 * @var string
	 */
	var $release_token;
	
	/**
	 * 平台内H矿卖出数
	 * 
	 * @var string
	 */
	var $sold_token;
	
	/**
	 * (单一用户)被邀请用户产生总上缴H币数
	 * 
	 * @var string
	 */
	var $invite_coin;
	
	/**
	 * 被邀请用户产生总H币数
	 * 
	 * @var string
	 */
	var $total_invite_coin;
	
	/**
	 * 一级被邀请用户总H币数
	 * 
	 * @var string
	 */
	var $total_user_coin_invite1;
	
	/**
	 * 一级被邀请用户获得H币上缴零级的比例
	 * 
	 * @var string
	 */
	var $user_coin_invite1_rate = 0.5;
	
	/**
	 * 二级被邀请用户总H币数
	 * 
	 * @var string
	 */
	var $total_user_coin_invite2;
	
	/**
	 * 二级被邀请用户获得H币上缴零级的比例
	 * 
	 * @var string
	 */
	var $user_coin_invite2_rate = 0.3;
	
	/**
	 * 三级被邀请用户总H币数
	 * 
	 * @var string
	 */
	var $total_user_coin_invite3;
	
	/**
	 * 三级被邀请用户获得H币上缴零级的比例
	 * 
	 * @var string
	 */
	var $user_coin_invite3_rate = 0.2;
	
	/**
	 * 总H矿数
	 * 
	 * @var string
	 */
	var $total_token;
	
	/**
	 * 用户注册获取的H矿总数
	 * 
	 * @var string
	 */
	var $user_token;
	
	/**
	 * 平台内交易H矿数
	 * 
	 * @var string
	 */
	var $exchange_token;
	
	/**
	 * 平台自持有H矿数
	 * 
	 * @var string
	 */
	var $own_token;
	
	/**
	 * 单个用户单日产生H币数
	 * 
	 * @var string
	 */
	var $daily_user_coin;
	
	/**
	 * 单个用户单日产生H矿数
	 * 
	 * @var string
	 */
	var $daily_user_token;
	
	/**
	 * 单个用户单日获取分红
	 * 
	 * @var string
	 */
	var $daily_user_bonus;
	
	/**
	 * 单个用户获取的H币总数
	 * 
	 * @var string
	 */
	var $user_total_coin;
	
	/**
	 * 所有用户产生总H币数
	 * 
	 * @var string
	 */
	var $total_user_coin;
	
	/**
	 * 单个用户获取的H矿数
	 * 
	 * @var string
	 */
	var $user_total_token;
	
	/**
	 * 所有用户持有总H矿数
	 * 
	 * @var string
	 */
	var $total_user_token;
	
	/**
	 * 单个用户获取总分红
	 * 
	 * @var string
	 */
	var $user_total_bonus;
	
	/**
	 * 所有用户获取总分红
	 * 
	 * @var string
	 */
	var $total_user_bonus;
	
	/**
	 * 所有作者持有总H矿数
	 * 
	 * @var string
	 */
	var $total_author_token;
	
	/**
	 * 单个作者持有H矿数
	 * 
	 * @var string
	 */
	var $author_total_token;
	
	/**
	 * 所有作者获取总分红
	 * 
	 * @var string
	 */
	var $total_author_bonus;
	
	/**
	 * 单个作者获取总分红
	 * 
	 * @var string
	 */
	var $author_total_bonus;
	
	/**
	 * 所有作者产生总H币数
	 * 
	 * @var string
	 */
	var $total_author_coin;
	
	/**
	 * 单个作者产生总H币数
	 * 
	 * @var string
	 */
	var $author_total_coin;
	
	/**
	 * 单个作者单日获取H矿数
	 * 
	 * @var string
	 */
	var $daily_author_token;
	
	/**
	 * 单个作者单日获取分红
	 * 
	 * @var string
	 */
	var $daily_author_bonus;
	
	/**
	 * 单个作者单日产生H币数
	 * 
	 * @var string
	 */
	var $daily_author_coin;
	
	/**
	 * 数据集合
	 * 
	 * @var array
	 */
	var $data = [ ];
	
	/**
	 * 构造函数
	 */
	function __construct() {
		// 设置所有bc数学函数的默认小数点保留位数
		bcscale ( 4 );
	}
	
	/**
	 * 设置数据对象的值
	 * 
	 * @access public
	 * @param string $name
	 *        	名称
	 * @param mixed $value
	 *        	值
	 * @return void
	 */
	public function __set($name, $value) {
		// 设置数据对象属性
		$this->data [$name] = $value;
	}
	
	// ************************************基础计算公式******************************************//
	
	/**
	 * 注册用户数
	 * 
	 * @return number
	 */
	public function TotalUser() {
		$this->total_user = bcmul ( $this->daily_register_user, $this->Time );
		return $this->total_user;
	}
	
	/**
	 * 总合格文章数
	 * 
	 * @return string
	 */
	public function TotalArticle() {
		$this->total_article = bcmul ( $this->daily_article, $this->Time );
		return $this->total_article;
	}
	
	/**
	 * H矿价格
	 * 
	 * @return number
	 */
	public function TokenPrice() {
// 		$this->token_price = bcdiv ( $this->total_produce_coin, $this->release_token );
// 		$this->token_price = bcmul ( $this->token_price, $this->token_price_param );

		$this->token_price = bcdiv($this->total_produce_coin, bcmul($this->total_user, $this->user_register_token));
		$this->token_price = bcmul ( $this->token_price, $this->token_price_param );
		return $this->token_price;
	}
	
	/**
	 * 总H币产生数
	 * 
	 * @return string
	 */
	public function TotalProduceCoin() {
		$vb = bcmul ( $this->visit, $this->bonus );
		$ff = bcmul ( $this->follow_visit, $this->follow_bonus );
		$vbff = bcadd ( $vb, $ff );
		$vbffi = bcadd ( $vbff, $this->invite_coin );
		$left_operand = bcmul ( $vbffi, bcmul ( $this->total_user, $this->Time ) );
		$right_operand = bcmul ( $this->daily_author_coin, bcmul ( $this->total_author, $this->Time ) );
		$this->total_produce_coin = bcadd ( $left_operand, $right_operand );
		return $this->total_produce_coin;
	}
	
	/**
	 * H矿总释放数
	 * 
	 * @return string
	 */
	public function ReleaseToken() {
		$this->release_token = bcadd ( bcmul ( $this->total_user, $this->user_register_token ), $this->sold_token );
		return $this->release_token;
	}

	/**
	 * 初始化 H矿总释放数 当 total_produce_coin*token_price_param == total_coin的时候
	 * @return string
	 */
	public function ReleaseTokenInit() {
		$this->release_token = bcmul ( $this->total_user, $this->user_register_token );
		return $this->release_token;
	}
	
	/**
	 * H矿总释放数 合计公式
	 * 
	 * @return string
	 */
	public function ReleaseTokenUA() {
		$this->release_token = bcadd ( $this->total_user_token, $this->total_author_token );
		return $this->release_token;
	}
	
	/**
	 * H矿总释放数 导出公式 基于 sold_token
	 */
	public function ReleaseTokenDiv() {
		$tpc = $this->total_produce_coin; 
		$tpp = $this->token_price_param;
		$tu = bcmul($this->total_user, $this->user_register_token);
		$bcmulTpcTppTu = bcmul($tpc, bcmul($tpp, $tu));
		$bcmulTpcTpp = bcmul($tpc, $tpp);
		$this->release_token = bcdiv($bcmulTpcTppTu, bcsub($bcmulTpcTpp, $this->total_coin));
		return $this->release_token;
	}
	
	/**
	 * H矿总释放数 一次方根导出公式
	 * 
	 * @return string
	 */
	public function ReleaseTokenBcsqrt() {
		$ututa = bcmul($this->user_register_token, bcadd($this->total_user, $this->total_author));
		$B = bcmul($this->total_produce_coin, $this->token_price_param);
		$A = bcmul($this->Time, bcadd(bcmul($this->daily_user_coin, $this->total_user), bcmul($this->daily_author_coin, $this->total_author)));
		$this->release_token = bcdiv(bcmul($ututa, $B), bcsub($B, $A));
		return $this->release_token;
	}
	
	/**
	 * 平台内H矿卖出数,通过release_token获取
	 * 
	 * @return string
	 */
	public function SoldTokenByReleaseToken() {
		$this->sold_token = bcsub ( $this->release_token, bcmul ( $this->total_user, $this->user_register_token ) );
		return $this->sold_token;
	}
	
	/**
	 * 平台内H矿卖出数
	 * 
	 * @return string
	 */
	public function SoldToken() {
		$this->sold_token = bcdiv ( $this->total_coin, $this->token_price );
		return $this->sold_token;
	}
	
	/**
	 * H币产生总数量
	 * 
	 * @return number
	 */
	public function TotalCoin() {
		$this->total_coin = bcadd ( $this->total_user_coin, $this->total_author_coin );
		return $this->total_coin;
	}
	
	/**
	 * H币兑换总数量
	 * 
	 * @return string
	 */
	public function TotalExchangeCoin() {
		$this->total_exchange_coin = $this->total_coin ;
		return $this->total_exchange_coin;
	}
	
	/**
	 * H币消费总数量
	 * 
	 * @return string
	 */
	public function TotalConsumeCoin() {
		$left = bcadd ( $this->tip, bcadd ( $this->alert, $this->other ) );
		$this->total_consume_coin = bcmul ( $left, bcmul ( $this->Time, $this->total_user ) );
		return $this->total_consume_coin;
	}
	
	/**
	 * H币主激励产生数
	 * 
	 * @return string
	 */
	public function TotalMainCoin() {
		$this->total_main_coin = bcmul ( bcmul ( $this->visit, $this->bonus ), bcmul ( $this->Time, $this->total_user ) );
		return $this->total_main_coin;
	}
	
	/**
	 * H币总投资收益数
	 */
	public function TotalFollowCoin() {
		$this->total_follow_coin = bcmul ( bcmul ( $this->follow_visit, $this->follow_bonus ), bcmul ( $this->Time, $this->total_user ) );
		return $this->total_follow_coin;
	}
	
	/**
	 * 成本总额(分红池)[每日]
	 * 
	 * @return string
	 */
	public function Fund() {
		$tup = bcmul ( $this->daily_register_user, $this->fund_user_price );
		$tia = bcmul ( $this->daily_article, $this->fund_article_price );
		$this->fund = bcadd ( $tup, $tia );
		return $this->fund;
	}
	
	/**
	 * (单一用户)被邀请用户产生总上缴H币数
	 * 
	 * @return string
	 */
	public function InviteCoin() {
		$t1 = bcmul ( $this->total_user_invite_1, $this->user_coin_invite1_rate );
		
		$t2 = bcmul ( $this->total_user_invite_2, bcadd ( $this->user_coin_invite1_rate, $this->user_coin_invite2_rate ) );
		
		$t3 = bcmul ( $this->total_user_invite_3, bcadd ( $this->user_coin_invite3_rate, bcadd ( $this->user_coin_invite1_rate, $this->user_coin_invite2_rate ) ) );
		
		$t = bcadd ( $t1, bcadd ( $t2, $t3 ) );
		
		$this->invite_coin = bcdiv ( bcmul ( $t, $this->daily_user_coin ), $this->total_user );
		return $this->invite_coin;
	}
	
	/**
	 * 通过用户单日产生H币数公式, 反向计算被邀请用户产生总上缴H币数
	 *
	 * @return string
	 */
	public function InviteCoinByDailyUserCoin() {
		$tip_alert_other = bcadd ( bcadd ( $this->tip, $this->alert ), $this->other );
		$vbffi = bcadd ( $this->daily_user_coin, $tip_alert_other );
		$vb = bcmul ( $this->visit, $this->bonus );
		$ff = bcmul ( $this->follow_visit, $this->follow_bonus );
		$vbff = bcadd ( $vb, $ff );
		$this->invite_coin = bcsub ( $vbffi, $vbff );
		return $this->invite_coin;
	}
	
	/**
	 * 通过用户单日产生H币数公式, 导出邀请用户产生总上缴H币数
	 */
	public function InviteCoinImportByDailyUserCoin() {
		$vb = bcmul ( $this->visit, $this->bonus );
		$ff = bcmul ( $this->follow_visit, $this->follow_bonus );
		$vbff = bcadd ( $vb, $ff );
		$tip_alert_other = bcadd ( bcadd ( $this->tip, $this->alert ), $this->other );
		$A = bcsub($vbff, $tip_alert_other);
		$t1 = bcmul ( $this->total_user_invite_1, $this->user_coin_invite1_rate );
		$t2 = bcmul ( $this->total_user_invite_2, bcadd ( $this->user_coin_invite1_rate, $this->user_coin_invite2_rate ) );
		$t3 = bcmul ( $this->total_user_invite_3, bcadd ( $this->user_coin_invite3_rate, bcadd ( $this->user_coin_invite1_rate, $this->user_coin_invite2_rate ) ) );
		$tui = bcadd ( $t1, bcadd ( $t2, $t3 ) );
		$this->invite_coin = bcdiv(bcmul($A, $tui), bcsub($this->total_user, $tui));
		
		return $this->invite_coin;
	}
	
	/**
	 * 被邀请用户产生总H币数
	 * 
	 * @return string
	 */
	public function TotalInviteCoin() {
		$this->total_invite_coin = bcmul ( $this->invite_coin, bcmul ( $this->Time, $this->total_user ) );
		return $this->total_invite_coin;
	}
	
	/**
	 * 被邀请用户总H币数
	 * 
	 * @return string
	 */
	public function TotalUserCoinInvite() {
		// 一级被邀请用户总H币数
		$this->total_user_coin_invite1 = bcmul ( bcmul ( $this->total_user_invite_1, $this->user_coin_invite1_rate ), bcmul ( $this->daily_user_coin, $this->Time ) );
		
		$this->total_user_coin_invite2 = bcmul ( bcmul ( $this->total_user_invite_2, bcadd ( $this->user_coin_invite1_rate, $this->user_coin_invite2_rate ) ), bcmul ( $this->daily_user_coin, $this->Time ) );
		
		$this->total_user_coin_invite3 = bcmul ( bcmul ( $this->total_user_invite_3, bcadd ( $this->user_coin_invite3_rate, bcadd ( $this->user_coin_invite1_rate, $this->user_coin_invite2_rate ) ) ), bcmul ( $this->daily_user_coin, $this->Time ) );
	}
	
	/**
	 * 用户单日产生H币数
	 * 
	 * @return string
	 */
	public function DailyUserCoin() {
		$vb = bcmul ( $this->visit, $this->bonus );
		$ff = bcmul ( $this->follow_visit, $this->follow_bonus );
		$vbff = bcadd ( $vb, $ff );
		$vbffi = bcadd ( $vbff, $this->invite_coin );
		$tip_alert_other = bcadd ( bcadd ( $this->tip, $this->alert ), $this->other );
		$this->daily_user_coin = bcsub ( $vbffi, $tip_alert_other );
		return $this->daily_user_coin;
	}
	
	
	/**
	 * 用户单日产生H矿数
	 * 
	 * @return string
	 */
	public function DailyUserToken() {
		
		$this->daily_user_token = bcdiv ( $this->daily_user_coin, $this->token_price, 20 );
		return $this->daily_user_token;
	}
	
	/**
	 * 用户单日获取分红
	 * 
	 * @return string
	 */
	public function DailyUserBonus() {
// 		var_export($this->fund);
// 		echo "$";
// 		var_export($this->user_total_token);
// 		echo "$";
// 		var_export(bcmul ( $this->fund, $this->user_total_token ));
// 		echo "$";
// 		var_export( $this->release_token );
// 		echo "$";
		$this->daily_user_bonus = bcdiv ( bcmul ( $this->fund, $this->user_total_token ), $this->release_token);
		//var_export( $this->daily_user_bonus );
		return $this->daily_user_bonus;
	}
	
	/**
	 * 单个用户产生总H币数
	 * 
	 * @return string
	 */
	public function UserTotalCoin() {
		$this->user_total_coin = bcmul ( $this->daily_user_coin, $this->Time );
		return $this->user_total_coin;
	}
	
	/**
	 * 所有用户产生总H币数
	 * 
	 * @return string
	 */
	public function TotalUserCoin() {
		$this->total_user_coin = bcmul ( $this->user_total_coin, $this->total_user );
		return $this->total_user_coin;
	}
	
	/**
	 * 单个用户获取的H矿数
	 * 
	 * @return string
	 */
	public function UserTotalToken() {
		$dT = bcmul ( $this->daily_user_token, $this->Time );
		$this->user_total_token = bcadd ( $this->user_register_token, $dT );
		return $this->user_total_token;
	}
	
	/**
	 * 所有用户持有总H矿数
	 * 
	 * @return string
	 */
	public function TotalUserToken() {
		$this->total_user_token = bcmul ( $this->user_total_token, $this->total_user );
		return $this->total_user_token;
	}
	
	/**
	 * 单个用户获取分红
	 * 
	 * @return string
	 */
	public function UserTotalBonus() {
		$this->user_total_bonus = bcmul ( $this->daily_user_bonus, $this->Time );
		return $this->user_total_bonus;
	}
	
	/**
	 * 所有用户获取总分红
	 * 
	 * @return string
	 */
	public function TotalUserBonus() {
		$this->total_user_bonus = bcmul ( $this->user_total_bonus, $this->total_user );
		return $this->total_user_bonus;
	}
	
	/**
	 * 单个作者单日产生H币数
	 * 
	 * @return string
	 */
	public function DailyAuthorCoin() {
		$tvb = bcmul ( $this->tip_visit, $this->bonus1 );
		$notvb = bcmul ( $this->no_tip_visit, $this->bonus2 );
		$this->daily_author_coin = bcadd ( $this->tip, bcadd ( $tvb, $notvb ) );
		return $this->daily_author_coin;
	}
	
	/**
	 * 单个作者单日获取H矿数
	 * 
	 * @return string
	 */
	public function DailyAuthorToken() {
		$this->daily_author_token = bcdiv ( $this->daily_author_coin, $this->token_price, 20 );
		return $this->daily_author_token;
	}
	
	/**
	 * 单个作者单日获取分红
	 * 
	 * @return string
	 */
	public function DailyAuthorBonus() {
		$ft = bcmul ( $this->fund, $this->author_total_token );
		$this->daily_author_bonus = bcdiv ( $ft, $this->release_token );
		return $this->daily_author_bonus;
	}
	
	/**
	 * 单个作者产生总H币数
	 * 
	 * @return string
	 */
	public function AuthorTotalCoin() {
		$this->author_total_coin = bcmul ( $this->daily_author_coin, $this->Time );
		return $this->author_total_coin;
	}
	
	/**
	 * 所有作者产生总H币数
	 * 
	 * @return string
	 */
	public function TotalAuthorCoin() {
		$this->total_author_coin = bcmul ( $this->author_total_coin, $this->total_author );
		return $this->total_author_coin;
	}
	
	/**
	 * 单个作者持有H矿数
	 * 
	 * @return string
	 */
	public function AuthorTotalToken() {
		if ( !$this->total_author) {
			return $this->author_total_token = '0.0000';
		}
		$dt = bcmul ( $this->daily_author_token, $this->Time );
		$this->author_total_token = bcadd ( $this->user_register_token, $dt );
		return $this->author_total_token;
	}
	
	/**
	 * 所有作者持有总H矿数
	 * 
	 * @return string
	 */
	public function TotalAuthorToken() {
		$this->total_author_token = bcmul ( $this->author_total_token, $this->total_author );
		return $this->total_author_token;
	}
	
	/**
	 * 单个作者获取总分红
	 * 
	 * @return string
	 */
	public function AuthorTotalBonus() {
		$this->author_total_bonus = bcmul ( $this->daily_author_bonus, $this->Time );
		return $this->author_total_bonus;
	}
	
	/**
	 * 所有作者获取总分红
	 * 
	 * @return string
	 */
	public function TotalAuthorBonus() {
		$this->total_author_bonus = bcmul ( $this->author_total_bonus, $this->total_author );
		return $this->total_author_bonus;
	}
}